package com.magic.smartcity.view.more.bus

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import com.magic.smartcity.R
import com.magic.smartcity.beans.BusOrderBean
import com.magic.smartcity.databinding.FragmentBusRidingPassengerBinding
import com.magic.smartcity.view.more.viewmodels.BusViewModel

class BusRidingPassengerFragment : Fragment() {

    private lateinit var binding: FragmentBusRidingPassengerBinding

    private val viewModel by lazy {
        ViewModelProvider(this).get(BusViewModel::class.java)
    }

    private var lineId:Int = -1
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        lineId = arguments?.getInt("lineId")!!
        viewModel.getLineStationListById(lineId!!)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentBusRidingPassengerBinding.inflate(layoutInflater)
        initView()
        return binding.root
    }

    private fun initView() {
        viewModel.busLineStationListById.observe(viewLifecycleOwner) {
            val distinctBy = it.map { data -> data.name }
            val arrayAdapter = ArrayAdapter(requireContext(), R.layout.item_base_text, distinctBy)
            binding.startPlace.setAdapter(arrayAdapter)
            binding.endPlace.setAdapter(arrayAdapter)
        }
        binding.goToShowOrder.setOnClickListener {
            when {
                binding.passengerName.text != null && binding.passengerName.text.isNullOrEmpty() -> {
                    binding.passengerName.error = "乘车人姓名不能为空"
                }
                binding.passengerPhoneNumber.text != null && binding.passengerPhoneNumber.text.isNullOrEmpty() -> {
                    binding.passengerPhoneNumber.error = "乘车人联系电话不能为空"
                }
                binding.startPlace.text != null && binding.startPlace.text.isNullOrEmpty() -> {
                    binding.startPlace.error = "上车地点不能为空"
                }
                binding.endPlace.text != null && binding.endPlace.text.isNullOrEmpty() -> {
                    binding.endPlace.error = "下车地点不能为空"
                }
                else -> {
                    binding.passengerName.error = null
                    binding.passengerPhoneNumber.error = null
                    binding.startPlace.error = null
                    binding.endPlace.error = null
                    val bundle = Bundle()
                    val busOrderBean:BusOrderBean = arguments?.getSerializable("busOrderBean") as BusOrderBean
                    busOrderBean.userName = binding.passengerName.text.toString()
                    busOrderBean.userTel = binding.passengerPhoneNumber.text.toString()
                    busOrderBean.start = binding.startPlace.text.toString()
                    busOrderBean.end = binding.endPlace.text.toString()
                    bundle.putSerializable("busOrderBean",busOrderBean)
                    bundle.putInt("lineId",lineId)
                    Navigation.findNavController(binding.root).navigate(R.id.action_busRidingPassengerFragment_to_busSubmitOrderFragment,bundle)
                }
            }
        }
    }

}